package com.ohuang.fakescreen

import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.*
import android.widget.TextView
import java.lang.ref.WeakReference

class MainActivity : AppCompatActivity() {
    private class MainActivityHandler(activity: MainActivity) :
        Handler() {
        private val mWeakReference: WeakReference<MainActivity>
        override fun handleMessage(msg: Message) {
            val activity: MainActivity? = mWeakReference.get()
            when (msg.what) {
                0 -> activity?.hideTv()
            }
        }

        init {
            mWeakReference = WeakReference<MainActivity>(activity)
        }
    }

    private var handler = MainActivityHandler(this)
    private var tv: TextView? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //全屏
        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN,

            )
        window.setFlags(
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
        ) //不灭屏

        setContentView(R.layout.activity_main)
        hideBottomNav()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            window?.insetsController!!.hide(WindowInsets.Type.statusBars())
        }
        tv = findViewById(R.id.tv_main)
        val view = findViewById<View>(R.id.cl_layout).setOnClickListener {
            tv?.visibility = View.VISIBLE
            handler.sendEmptyMessageDelayed(0, 3000L)
        }
    }

    private fun hideBottomNav() {
        window.getDecorView().setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    or View.SYSTEM_UI_FLAG_LOW_PROFILE
                    or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
                    or View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                    or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        )

    }

    fun hideTv() {
        tv?.visibility = View.GONE
    }

    override fun onDestroy() {
        super.onDestroy()
        handler.removeMessages(0)
    }
}